package org.jeecg.modules.demo.machineShop.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.machineShop.entity.*;
import org.jeecg.modules.demo.machineShop.service.*;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;


/**
 * App数据接口
 */

@Api(tags="首页数据接口")
@RestController
@RequestMapping("/machineShop/App")
@Slf4j
public class AppController extends JeecgController<Device, IDeviceService> {
   @Autowired
   private IDeviceService deviceService;

    @Autowired
    private ISysUserService sysUserService;

   @Autowired
   private INoticeService iNoticeService;//公告信息 App

    @Autowired
    private IPersonnelService iPersonnelService;//员工信息 App

    @Autowired
    private IViewProSalesNumService iViewProSalesNumService;//产品销售数量统计 App

    @Autowired
    private IViewOrdProvinceNumsService iViewOrdProvinceNumsService;//顾客地区分布数量统计 App

    @Autowired
    private IViewFauSumsService iViewFauSumsService;//2021年故障信息数量统计 App

    @Autowired
    private IViewSignSumService iViewSignSumService;//签到状态统计 App

    @Autowired
    private IOrderInfoService iOrderInfoService;//订单信息

    @Autowired
    private IFaultInfoService iFaultInfoService;//故障信息

    @Autowired
    private IDeviceService iDeviceService;//故障信息

    @Autowired
    private RedisUtil redisUtil;


    @GetMapping(value = "/app_notice_list")
    public Result<?> app_notice_list(){
        List<Notice> list_app_wor = iNoticeService.list();
        return Result.OK(list_app_wor);//返回数据
    }

    @GetMapping(value = "/app_notice_list/get")
    public Result<?> app_notice_list_get(@RequestParam(name="id",required=true) String id){
        Notice model = iNoticeService.getById(id);
        return Result.OK(model);//返回数据
    }

    //员工列表--------------------------------------------------------------------------------------------
    @GetMapping(value = "/app_personnel_list")
    public Result<?> app_personnel_list(){
        List<Personnel> list_app_wor = iPersonnelService.list();
        return Result.OK(list_app_wor);//返回数据
    }

    @GetMapping(value = "/app_personnel_list/get")
    public Result<?> app_personnel_list_get(@RequestParam(name="id",required=true) String id){
        Personnel model = iPersonnelService.getById(id);
        return Result.OK(model);//返回数据
    }

    //产品销售统计--------------------------------------------------------------------------------------------
    @GetMapping(value = "/app_ord_sale_list")
    public Result<?> app_ord_sale_list(){
        List<ViewProSalesNum> list_rod_sale_list = iViewProSalesNumService.list();
        return Result.OK(list_rod_sale_list);//返回数据
    }

    @GetMapping(value = "/app_ord_sale_list/get")
    public Result<?> app_ord_sale_list_get(@RequestParam(name="id",required=true) String id){
        ViewProSalesNum model = iViewProSalesNumService.getById(id);
        return Result.OK(model);//返回数据
    }

    //2021年故障信息统计--------------------------------------------------------------------------------------------
        @GetMapping(value = "/app_fau_list")
    public Result<?> app_fau_list(){
        List<ViewFauSums> list_fau_list = iViewFauSumsService.list();
        return Result.OK(list_fau_list);//返回数据
    }

    @GetMapping(value = "/app_fau_list/get")
    public Result<?> app_fau_list_get(@RequestParam(name="id",required=true) String id){
        ViewFauSums model = iViewFauSumsService.getById(id);
        return Result.OK(model);//返回数据
    }

    //员工分布地区数量统计--------------------------------------------------------------------------------------------
    @GetMapping(value = "/app_ord_province_list")
    public Result<?> app_ord_province_list(){
        List<ViewOrdProvinceNums> list_ord_province_list = iViewOrdProvinceNumsService.list();
        return Result.OK(list_ord_province_list);//返回数据
    }

    @GetMapping(value = "/app_ord_province_list/get")
    public Result<?> app_ord_province_list_get(@RequestParam(name="id",required=true) String id){
        ViewOrdProvinceNums model = iViewOrdProvinceNumsService.getById(id);
        return Result.OK(model);//返回数据
    }

    //员工签到状态统计--------------------------------------------------------------------------------------------
    @GetMapping(value = "/app_per_sign_list")
    public Result<?> app_per_sign_list(){
        List<ViewSignSum> list_per_sign_list = iViewSignSumService.list();
        return Result.OK(list_per_sign_list);//返回数据
    }

    @GetMapping(value = "/app_per_sign_list/get")
    public Result<?> app_per_sign_list_get(@RequestParam(name="id",required=true) String id){
        ViewSignSum model = iViewSignSumService.getById(id);
        return Result.OK(model);//返回数据
    }

    //订单信息----------------------------------------------------------------------------------------------------
    @GetMapping(value = "/app_ord_datalist")
    public Result<?> app_ord_datalist(){
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        Page<OrderInfo> page = new Page<OrderInfo>(0, 10000);
        IPage<OrderInfo> pageList = iOrderInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    //故障信息----------------------------------------------------------------------------------------------------
    @GetMapping(value = "/app_fau_datalist")
    public Result<?> app_fau_datalist(){
        QueryWrapper<FaultInfo> queryWrapper = new QueryWrapper<>();
        Page<FaultInfo> page = new Page<FaultInfo>(0, 10000);
        IPage<FaultInfo> pageList = iFaultInfoService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    //设备信息----------------------------------------------------------------------------------------------------
    @GetMapping(value = "/app_dev_datalist")
    public Result<?> app_dev_datalist(){
        List<Device> list_dec = iDeviceService.list();
        return Result.OK(list_dec);//返回数据
    }


    //App登录接口
    @RequestMapping("/appLogin")
    public Result login(@RequestParam(name = "account")String account,@RequestParam(name = "password" )String password){
        Result result = new Result();
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername,account);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        //判断用户输入的账号是否存在
        if (sysUser == null){
            result.setCode(66666);
            result.setMessage("您输入的账号有误");
            return result;
        }
        //校验用户名或密码是否正确
        String userpassword = PasswordUtil.encrypt(account, password, sysUser.getSalt());
        String syspassword = sysUser.getPassword();
        if (!syspassword.equals(userpassword)) {
            result.setCode(66666);
            result.setMessage("用户名或密码错误");
            return result;
        }


        JSONObject obj = new JSONObject();
        obj.put("userInfo", sysUser);
        String token = JwtUtil.sign(account, syspassword);
        // 设置token缓存有效时间
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);
        obj.put("token", token);
        result.setResult(obj);
        
        System.out.println(sysUser.getUsername());

        //到这里就登陆成功啦
        result.setCode(200);
        result.setMessage("登录成功");
        return result;
    }

    //App注册接口
    @RequestMapping("/appReg")
    public Result reg(@RequestParam(name = "account")String account,
                      @RequestParam(name = "password" )String password,
                      @RequestParam(name = "email" )String email) {

        Result result = new Result();

        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUser::getUsername,account);
        SysUser sysUser = sysUserService.getOne(queryWrapper);
        //判断用户输入的账号是否存在
        if (sysUser == null){
            //没有这个账号就能注册
            SysUser user = new SysUser();
            try {
                user.setCreateTime(new Date());// 设置创建时间
                String salt = oConvertUtils.randomGen(8);
                String passwordEncode = PasswordUtil.encrypt(account, password, salt);
                user.setSalt(salt);
                user.setUsername(account);
                user.setRealname(account);
                user.setPassword(passwordEncode);
                user.setEmail(email);
                user.setStatus(CommonConstant.USER_UNFREEZE);
                user.setDelFlag(CommonConstant.DEL_FLAG_0);
                user.setActivitiSync(CommonConstant.ACT_SYNC_0);
                sysUserService.addUserWithRole(user,"ee8626f80f7c2619917b6236f3a7f02b");//默认临时角色 test
                result.setCode(200);
                result.success("注册成功");
            } catch (Exception e) {
                result.setCode(66666);
                result.error500("注册失败");
            }

        }
        result.setCode(66666);
        result.setMessage("账号已存在");
        return result;
    }
}
